<template>
	<cl-dialog
		title="修改组员目标"
		width="500px"
		:visible.sync="visible"
		:before-close="beforeClose"
		@close="close"
		:props="{
			appendToBody: true,
			closeOnClickModal: false
		}"
	>
		<el-form ref="form" label-position="top" :model="form" :rules="rules" :disabled="saving">
			<el-form-item label="加微数(月)" prop="vorder_total">
				<el-input v-model.number="form.vorder_total" />
			</el-form-item>
			<el-form-item label="加微数(日)" prop="vorder_day">
				<el-input v-model.number="form.vorder_day" />
			</el-form-item>
			<el-form-item label="新客定金数(月)" prop="old_new_vorder_total">
				<el-input v-model.number="form.old_new_vorder_total" />
			</el-form-item>
			<el-form-item label="新客定金数(日)" prop="old_new_vorder_day">
				<el-input v-model.number="form.old_new_vorder_day" />
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="close">关闭</el-button>
			<el-button type="primary" @click="submit" :loading="saving">确定</el-button>
		</template>
	</cl-dialog>
</template>
<script>
import { validate } from "@/utils/validate";
export default {
	name: "edit-group-task",
	data() {
		return {
			visible: false,
			saving: false,
			form: {
				team_id: "",
				vorder_total: 0,
				vorder_day: 0,
				old_new_vorder_total: 0,
				old_new_vorder_day: 0
			},
			rules: {
				vorder_total: [
					{ required: true, message: "请填写加微数(月)" },
					{ validator: validate.targetVal("目标不能为0") }
				],
				vorder_day: [
					{ required: true, message: "请填写加微数(日)" },
					{ validator: validate.targetVal("目标不能为0") }
				],
				old_new_vorder_total: [
					{ required: true, message: "请填写定金数(月)" },
					{ validator: validate.targetVal("目标不能为0") }
				],
				old_new_vorder_day: [
					{ required: true, message: "请填写定金数(日)" },
					{ validator: validate.targetVal("目标不能为0") }
				]
			}
		};
	},
	methods: {
		open(data = {}) {
			this.form = { ...data };
			this.visible = true;
		},
		beforeClose(done) {
			!this.saving && done();
		},
		close() {
			this.visible = false;
			this.reset();
		},
		reset() {
			this.saving = false;
			this.$refs.form.resetFields();
		},
		submit() {
			this.$refs.form.validate(async (valid, errors) => {
				this.showFormErr(errors);
				if (!valid) return;
				try {
					this.saving = true;
					await this.$service.preSale.teamtargetedit({
						team_id: this.form.team_id,
						jvs_month: this.form.vorder_total, //加微数月
						jvs_day: this.form.vorder_day, //加微数日
						deposit_month: this.form.old_new_vorder_total, //定金数月
						deposit_day: this.form.old_new_vorder_day //定金数日
					});
					this.$message.success("操作成功");
					this.$emit("confirm");
					this.saving = false;
					this.close();
				} catch (error) {
					this.saving = false;
				}
			});
		}
	}
};
</script>
